<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html class="x-admin-sm">
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	request.setAttribute("basePath", basePath);
%>
<head>
<meta charset="UTF-8">
<title>仓库管理</title>
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/font.css">
<link rel="stylesheet"
	href="<%=request.getContextPath()%>/css/xadmin.css">
<script type="text/javascript"
	src="<%=request.getContextPath()%>/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/js/xadmin.js"></script>
</head>
<body>
	<div class="x-nav">
		<span class="layui-breadcrumb"> <a href="">基础数据</a> <a> <cite>仓库信息管理</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			onclick="location.reload()" title="刷新"> <i
			class="layui-icon layui-icon-refresh" style="line-height: 30px"></i>
		</a>
	</div>
	<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
			<div class="layui-col-md12">
				<div class="layui-card">
					<div class="layui-card-body ">
						<form class="layui-form layui-col-space5">
							<div class="layui-inline layui-show-xs-block">
								<input type="text" name="address" placeholder="仓库地址"
									autocomplete="off" class="layui-input">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<input type="checkbox" id="isDel_id" checked="" name="isDel" lay-skin="switch"
									lay-filter="switchTest" lay-text="可用|不可用">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<button class="layui-btn" lay-submit="" lay-filter="sreach">
									<i class="layui-icon">&#xe615;</i>
								</button>
							</div>
						</form>
					</div>
					<div class="layui-card-body ">
						<table class="layui-table" lay-filter="layui_table_list" id="test"></table>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
<script type="text/html" id="toolbarDemo">
	<div class = "layui-btn-container" > 
		<button class = "layui-btn layui-btn-sm" lay-event = "add" > 新增 </button>
		<button class="layui-btn layui-btn-sm" lay-event="edit">修改</button > 
		<button class = "layui-btn layui-btn-sm" lay-event = "delete" > 删除</button>
	</div > 
</script>
<script>
// https://blog.csdn.net/DCFANS/article/details/91895820
	layui.use([ 'table', 'layer', 'jquery', 'laypage', 'form' ], function() {
		var table = layui.table, layer = layui.layer, $=layui.$, laypage = layui.laypage, form = layui.form;
		
		var index = layer.msg('数据正从火星赶来...', {icon: 16, time: 10000, shade: 0});
		var param = {}
		
		//表初始化
       	table.render({
       		id : 'listReload',
       		elem : '#test',
       		toolbar : '#toolbarDemo',
       		cols : [[
       			{checkbox:true},
       			{field:'id', title: 'ID'},
       			{field:'address', sort: true, title: '仓库地址'},
       			{field:'isDel', sort: true, title: '是否可用', templet: function (d) {
       				if (d.isDel == 1){
                        return '可用';
                    }else {
                        return '不可用';
                    }
				}},
       			{field:'desc', title: '描述'}
               ]],
       		method : 'POST',
       		url: '<%=basePath%>/respository/queryRespositorys',
			contentType : 'application/json;charset=utf-8',
			page : true,
			loading: true, // 翻页加loading
			limits : [ 10, 15, 20, 50 ],
			request : {
				pageName : 'pageNumber',
				limitName : 'pageSize'
			},
			where : param,
			response : {
				statusName : 'errCode', //状态字段名称
				statusCode : '200', //状态字段成功值
				msgName : 'errMsg', //消息字段
				countName : 'count', //总数字段
				dataName : 'data' //数据字段
			},
			done: function(res){
				layer.close(index);
			}
		});
		
		//新增修改删除
		table.on('toolbar(layui_table_list)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id),
			data = checkStatus.data;
			data = eval("(" + JSON.stringify(data) + ")");
			switch(obj.event) {
				case 'add':
					var index = layer.open({
						type: 2,
						title: "新增仓库信息页面",
						area: ['30%', '50%'],
						fix: false,
						maxmin: true,
						shadeClose: true,
						shade: 0.4,
						skin: 'layui-layer-rim',
						content: ["<%=basePath%>/toRespositoryAdd", "no"]
					});
					break;
				case 'edit':
					if (data.length <= 0) {
						layer.msg("请先选中要修改的行", {
							icon: 6,
							timeout: 300
						});
						break;
					}
					var index = layer.open({
						type: 2,
						title: "修改仓库信息页面",
						area: ['30%', '50%'],
						fix: false,
						maxmin: true,
						shadeClose: true,
						shade: 0.4,
						skin: 'layui-layer-rim',
						content: ["<%=basePath%>/toRespositoryUpdate", "no"],
						success: function(layero, index){
							var body=layer.getChildFrame('body',index);
							body.contents().find("#id").val(data[0].id);
							body.contents().find("#address").val(data[0].address);
							if (data[0].isDel) {
								body.contents().find("#isDelId").html('<input type="checkbox" name="isDelStr" id="isDelStr" lay-text="可用|不可用" checked="checked" lay-skin="switch"><div class="layui-unselect layui-form-switch layui-form-onswitch" lay-skin="_switch"><em>可用</em><i></i></div>');
							} else {
								body.contents().find("#isDelId").html('<input type="checkbox" name="isDelStr" id="isDelStr" lay-text="可用|不可用" lay-skin="switch"><div class="layui-unselect layui-form-switch layui-form-onswitch" lay-skin="_switch"><em>可用</em><i></i></div>');
							}
							body.contents().find("#desc").val(data[0].desc);
				        },
					});
					break;
				case 'delete':
					var delIndex = layer.confirm('确定要删除选中的仓库信息吗?', function(delIndex) {
						var list = [];
						for (var i = 0; i < data.length; i++) {
							list.push(data[i].id);
						}
						if (list.length <= 0) {
							layer.close(delIndex);
							layer.msg("请先选中要删除的行", {
								icon: 6,
								timeout: 300
							});
						} else {
							$.ajax({
								headers: {
							        'Content-Type': 'application/json'
							    },
								url: '<%=basePath%>respository/deleteRespositorys',
								type: "post",
								data: JSON.stringify(list),
								success: function(suc) {
									if(suc.errCode == 200) {
										if (suc.data) {
											layer.close(delIndex);
											layer.msg("删除成功", {
												icon: 1,
												time: 200
											}, function(){
												location.reload();
											});
										} else {
											layer.msg("请先选中要删除的行", {
												icon: 5,
												time: 200
											}, function(){
												location.reload();
											});
										}
									} else {
										layer.msg("删除失败", {
											icon: 5,
											time: 200
										}, function(){
											location.reload();
										});
									}
								}
							});
						}
						layer.close(delIndex);
					});
					break;
			}
		});

		form.render();
		
		// 条件查询
		form.on('submit(sreach)',function(data){
			console.log(data);
			var formData = data.field;
			var address = formData.address;
			var isDel = 1;
			if($("#isDel_id").is(':checked')){
				isDel = 1;
			} else {
				isDel = 0;
			}
			var par = {
					address: address,
					isDel: isDel
		        }
			table.reload('listReload', {
				page: {
					curr: 1
				},
				where: par
			});
			return false;
		});
	});
</script>
</body>
</html>